package OliveXML;

# Copyright (c) 2005, Shawn Boyette
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in
#   the documentation and/or other materials provided with the
#   distribution.
#  * Neither the name of the Olive Newsreader  nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

require Exporter;
use warnings;
use strict;
use Date::Calc qw(Mktime Timezone);
use Digest::MD5 qw(md5_hex);
use Encode qw(encode_utf8);

our @ISA       = qw(Exporter);
our @EXPORT    = qw( &parseitems );

my %months = ( 'Jan' => 1,
               'Feb' => 2, 
               'Mar' => 3, 
               'Apr' => 4, 
               'May' => 5, 
               'Jun' => 6, 
               'Jul' => 7, 
               'Aug' => 8, 
               'Sep' => 9, 
               'Oct' => 10, 
               'Nov' => 11, 
               'Dec' => 12
             );

my %z = ( 'GMT' => 0,
          'UTC' => 0,
          'WET' => 0,
          'Z'   => 0,
          'JST' => 9,
          'EST' => -5,
          'EDT' => -4,
          'CST' => -6,
          'CDT' => -5,
          'MST' => -7,
          'MDT' => -6,
          'PST' => -8,
          'PDT' => -7,
          'AKST'=> -9,
          'AKDT'=> -8,
        );

sub parseitems {
    my ($items,$cui,$nick) = @_;
    my $c = $cui->userdata->{c};
    my $d = $cui->userdata->{dbh};
    my $w = $cui->userdata->{wins};
    my %md5s = ();
    my $count = @{$items};
    my $i = 1;
    
    foreach my $item (@{$items}) {
        my @q = qw(NULL NULL NULL NULL NULL NULL NULL NULL);
        $q[0] = $d->quote($nick);
        
        if ($item->{pubDate}) {
            # turn the RFC-822 date into seconds-since-epoch
            # Thu, 14 Apr XX05 10:16:24 GMT
            my $os = 0; # offset for 2-year dates
            my $rfc822 = $item->{pubDate};
            my $year = substr($rfc822,12,4);
            $os = 2 if $year =~ /\s/;
            if ($year =~ /\s/) {
                $year = substr($rfc822,12,2);
                $year = "20" . $year;
            }
            my $mon  = $months{substr($rfc822,8,3)};
            my $day  = substr($rfc822,5,2);
            my $hour = substr($rfc822,(17 - $os),2);
            my $min  = substr($rfc822,(20 - $os),2);
            my $sec  = substr($rfc822,(23 - $os),2);
            my $tz   = substr($rfc822,(26 - $os),3);
            $q[1] = Mktime($year,$mon,$day,$hour,$min,$sec);
            $q[1] = $q[1] + (3600 * $z{$tz}) if $z{tz}; # TZ correction
        } elsif ($item->{'dc:date'}) {
            # turn the ISO-8601 date into seconds-since-epoch
            # 2005-04-14T12:38:51-05:00
            my $iso8601 = $item->{'dc:date'};
            my $year = substr($iso8601,0,4);
            my $mon  = substr($iso8601,5,2);
            my $day  = substr($iso8601,8,2);
            my $hour = substr($iso8601,11,2);
            my $min  = substr($iso8601,14,2);
            my $sec  = substr($iso8601,17,2);
            my $tz   = substr($iso8601,20,2);
            $q[1] = Mktime($year,$mon,$day,$hour,$min,$sec);
            $q[1] = $q[1] + (3600 * $tz) if $tz; # TZ correction
        } else {
            # no time given. set to epoch.
            $q[1] = 0;
        }

        $q[3] = 0; # 'read' flag
        $q[4] = $d->quote($item->{link}) if $item->{link};
        $q[5] = $d->quote($item->{title}) if $item->{title}; 
        
        # mogrify main text
        if ($item->{description}) {
            $q[6] = HTMangLe($item->{description});
            $q[6] = $d->quote($q[6]);
        }

        # build string for md5sum
        my $digeststring = $nick . $q[5] . $q[6];
        # which must be wrapped in UTF-8 encoding to deal with
        # chars above 255
        $q[2] = md5_hex(encode_utf8($digeststring));
        # store a copy in %md5s for later
        $md5s{$q[2]} = 1;
        $q[2] = $d->quote($q[2]);
        
        # check for matching MD5 TODO
        my ($xid) = $d->selectrow_array("SELECT id FROM stories WHERE nick = $q[0] and md5 = $q[2]");
        # only insert if no match
        unless ($xid) {
            my $statement = "INSERT into STORIES values (NULL, $q[0], $q[1], "
                . "$q[2], $q[3], $q[4], $q[5], $q[6])";
            #$cui->dialog($statement);
            my $sth = $d->prepare($statement);
            $sth->execute;
        }
        percent($i,$count,$c->{feeds}{$nick}{disp},$w);
        $i++;
    }
    return \%md5s;
}

#-------------------------------------------------------------

sub percent {
    my ($i,$count,$nick,$w) = @_;

    my $percent = int(($i / $count) * 100);

    my $msg = "Parsing $nick [ $i of $count ($percent\%)]";
    $w->{news}{ftr1}->text($msg . ' ' x ($w->{dim}[0] - length($msg)));
    $w->{news}{ftr1}->draw;
}

#-------------------------------------------------------------

sub HTMangLe {
    my $text = shift;
    # reformatting
    $text =~ s|</p>||g;
    $text =~ s|</?span.*?>||g;
    $text =~ s|</?font.*?>||g;
    $text =~ s/<img.*?src=["'](.*?)["'].*?>/\[img: $1\]/g;
    $text =~ s#</?(?:strong|b)>#\*#g;
    $text =~ s#</?(?:em|i)>#\*#g;
    $text =~ s|<a href=["'](.*?)["']>(.*?)</a>|$2 \[$1\]|g;
    $text =~ s|</a>||g;
    # newline munching
    $text =~ s/\n{1,1}//mg;
    $text =~ s/<p>/\n\n/mg;
    $text =~ s/<br ?\/?>/\n/mg;
    $text =~ s/^[\s\n]+//m;
    $text =~ s/\n{3,}/\n\n/mg;
    return $text;
}



1;
